//
//  ClinicCardController.m
//  EcmDoctor
//
//  Created by 张进 on 15/7/13.
//  Copyright (c) 2015年 张进. All rights reserved.
//

#import "ClinicCardController.h"
#import "ClinicCardView.h"
#import "Clinic.h"
#import "AppUtils.h"
#import <SSKeychain.h>
#import "badgeUrl.h"
#import "AppDelegate.h"

@interface ClinicCardController (){
    NSMutableArray *imageArray;//用于装需要展示的图片
    float sWidth;
    float sHeight;
}

@property (nonatomic, strong) PagedFlowView *pageFlowView;
@property (nonatomic, strong) UIButton *touchButton;
@property (nonatomic, strong) UILabel *lab;
@property (nonatomic, strong) UIView *blackView;
@property (nonatomic, assign) BOOL delete;

@end

@implementation ClinicCardController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    
    sWidth = self.view.frame.size.width;
    CGFloat ty =self.navigationController.tabBarController.tabBarController.navigationController.navigationBar.frame.origin.y;
    CGFloat topBarsViewHeight = self.navigationController.navigationBar.frame.size.height;
    sHeight = self.view.frame.size.height - topBarsViewHeight;
    
    self.navigationController.navigationBar.hidden = YES;
    
    imageArray  = [[NSMutableArray alloc] initWithCapacity:0];
//    [imageArray addObject:@"1.jpg"];
//    [imageArray addObject:@"3.jpg"];
//    [imageArray addObject:@"5.jpg"];
//    [imageArray addObject:@"7.jpg"];
    
    double appWidth = [UIScreen mainScreen].applicationFrame.size.width;
    double appHeight = [UIScreen mainScreen].applicationFrame.size.height;
    
    _pageFlowView = [[PagedFlowView alloc] initWithFrame:CGRectMake(0, -ty, appWidth, appHeight)];
    _pageFlowView.delegate = self;
    _pageFlowView.dataSource = self;
    _pageFlowView.minimumPageAlpha = 0.3;
    _pageFlowView.minimumPageScale = 0.9;
    
    [self.view addSubview:_pageFlowView];

    
    //当没有数据时的提示
    _touchButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    [_touchButton addTarget:self action:@selector(download) forControlEvents:UIControlEventTouchUpInside];
    
    _lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
    _lab.center = CGPointMake(kScreenWidth/2, kScreenHeight/2);
    _lab.text = @"暂时没有电话问诊信息，点击刷新";
    _lab.textAlignment = NSTextAlignmentCenter;
    
    [self download];
    
}

- (void)download {
    NSString *doctorId = [SSKeychain passwordForService:APP_SERVICE_NAME account:APP_DOCTORINFOID_KEY];
    if (!_blackView) {
        _blackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    }
    _blackView.backgroundColor = [UIColor blackColor];
    _blackView.alpha = 0.3;
    [self.view addSubview:_blackView];
    
    [Clinic loginWithDoctor_info_id:doctorId Isfinish:@"0" success:^(NSArray *doctor) {
        
        for (Clinic *info in doctor) {
            [imageArray addObject:info];
        }
        
        [_lab removeFromSuperview];
        [_touchButton removeFromSuperview];
        if (imageArray.count == 0) {
            [self.view addSubview:_lab];
            [self.view addSubview:_touchButton];
        }
        
        [_pageFlowView reloadData];
        [_blackView removeFromSuperview];
        //        _count = doctor.count;
        
    } failure:^(NSError *error) {
        NSLog(@"%@", error.localizedDescription);
        [_blackView removeFromSuperview];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) {
        
        //需要注意的是self.isViewLoaded是必不可少的，其他方式访问视图会导致它加载 ，在WWDC视频也忽视这一点。
        
        if (self.isViewLoaded && !self.view.window)// 是否是正在使用的视图
        {
            // Add code to preserve data stored in the views that might be
            // needed later.
            
            // Add code to clean up other strong references to the view in
            // the view hierarchy.
            self.view = nil;// 目的是再次进入时能够重新加载调用viewDidLoad函数。
        }
        
    }
    // Dispose of any resources that can be recreated.
}

- (void)viewWillDisappear:(BOOL)animated {
    
    
    
}

#pragma PagedFlowViewDelegate
-(CGSize)sizeForPageInFlowView:(PagedFlowView *)flowView{
    return CGSizeMake(sWidth-60, sHeight-160);
}
- (void)flowView:(PagedFlowView *)flowView didTapPageAtIndex:(NSInteger)index{
    
    Clinic *clinic = imageArray[index];
    
    if ([clinic.info_state isEqualToString:@"0"]) {
        clinic.info_state=@"1";
        [imageArray replaceObjectAtIndex:index withObject:clinic];
        //更新badge
        [badgeUrl updateOnLineStateWithmethodname:@"updateOnAddTimeState" andask_info_id:clinic.ask_info_id success:^(BOOL bl) {
            NSLog(@"success");
            [self upDateBadgemethod:@"getOnAddTimeInfoNum"];
        } failure:^(NSError *error) {
            
        }];
    }
    
    if (clinic.picUrl) {
        [self.tabBarController performSegueWithIdentifier:@"showClinicContrillerDetail" sender:@{@"name":clinic.name, @"sex":clinic.sex, @"content":clinic.content,@"voiceUrl":clinic.voiceUrl, @"age":clinic.age, @"picUrl":clinic.picUrl, @"headImage":clinic.icon_Url, @"info":[NSString stringWithFormat:@"患者已经预约了 %@ %@ %@的门诊加号", clinic.info_order_date, [AppUtils weekForClinic:clinic.info_order_week], clinic.info_order_time], @"doctorId":[SSKeychain passwordForService:APP_SERVICE_NAME account:APP_DOCTORINFOID_KEY], @"askId":clinic.ask_info_id}];
    } else {
        [self.tabBarController performSegueWithIdentifier:@"showClinicContrillerDetail" sender:@{@"name":clinic.name, @"sex":clinic.sex, @"content":clinic.content, @"voiceUrl":clinic.voiceUrl, @"age":clinic.age, @"headImage":clinic.icon_Url, @"info":[NSString stringWithFormat:@"患者已经预约了 %@ %@ %@的门诊加号", clinic.info_order_date, [AppUtils weekForClinic:clinic.info_order_week], clinic.info_order_time], @"doctorId":[SSKeychain passwordForService:APP_SERVICE_NAME account:APP_DOCTORINFOID_KEY], @"askId":clinic.ask_info_id}];
    }
}


#pragma PagedFlowViewDataSource
-(NSInteger)numberOfPagesInFlowView:(PagedFlowView *)flowView{
    return [imageArray count];
}

-(UIView *)flowView:(PagedFlowView *)flowView cellForPageAtIndex:(NSInteger)index{
    
    ClinicCardView *dView = [ClinicCardView instanceClinicCardViewWithSize:CGSizeMake(sWidth-60, sHeight-160)];
    dView.clinicModel = imageArray[index];
    
    return dView;
}

-(void)viewWillAppear:(BOOL)animated {
    
    
//    self.automaticallyAdjustsScrollViewInsets = NO;
    
}

#pragma mark 更新badge值
- (void)upDateBadgemethod:(NSString *)method {
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    NSString *docId = [SSKeychain passwordForService:APP_SERVICE_NAME account:APP_DOCTORINFOID_KEY];
    [badgeUrl badgeWithdoctor_info_id:docId moduleName:@"DoctorRS" methodname:method success:^(NSString *badgeNumber) {
        
        app.clinicBadge.text = badgeNumber;
        
    } failure:^(NSError *error) {
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
